﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Runda.CMS.Order.Entity;
using Runda.CMS.Common.Utility;
using System.Linq.Expressions;
using System.Data.Linq;

namespace Runda.CMS.Order.Repository
{
    public class ShippingRepository
    {
         private string db = ConfigurationManager.ConnectionStrings["OrderConnectionString"].ConnectionString;

         public List<Shipping> findShipping(String shippingRequestID,String inventoryCode,String customerID,String orderID)  
         {
             using (OrderDataContext context = new OrderDataContext(db))
             {
                 var od = from shipping in context.shipping
                          where (orderID == ""? true:shipping.orderId == orderID) &&
                                        (inventoryCode == "" ? true : shipping.inventoryCode == inventoryCode) &&
                                         (shippingRequestID == "" ? true : shipping.shippingRequestId == shippingRequestID) &&
                                         (customerID  == ""? true : shipping.customerID == customerID)
                          select shipping;
                 return od.ToList<Shipping>();
             }
         }
         public Object findShippingsByShippingId(String shippingId, int pageSize, int pageIndex, out int recordCount)
         {
             using (OrderDataContext context = new OrderDataContext(db))
             {
                 var od = from shipping in context.shipping
                          where (shippingId == "" ? true : shipping.sendBillNo == shippingId)
                          group shipping by new { shipping.sendBillNo, shipping.sendCompany, shipping.customerID,shipping.receivedDate,shipping.receivedMan,shipping.preReceivedDate } into shipping
                          join customer in context.customer
                          on shipping.Key.customerID equals customer.id
                          select new { shipping.Key.customerID, shipping.Key.sendBillNo, shipping.Key.sendCompany,shipping.Key.receivedDate,shipping.Key.receivedMan,shipping.Key.preReceivedDate,customerName = customer.name };
                 recordCount = od.Count();
                 return od.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
             }
         }
         public Object findShippingByOrderId(String orderId) {
             using (OrderDataContext context = new OrderDataContext(db))
             {
                 var od = from shipping in context.shipping
                          where shipping.orderId == orderId
                          group shipping by new { shipping.sendBillNo, shipping.orderId,shipping.sendCompany,shipping.receivedMan,shipping.receivedDate,shipping.preReceivedDate } into shipping
                          select new { shipping.Key.sendBillNo,shipping.Key.orderId,shipping.Key.sendCompany,shipping.Key.receivedMan,shipping.Key.receivedDate,shipping.Key.preReceivedDate};
                 return od.ToList();
             }
         }

         public List<ShippingRequest> findShippingRequestByShippingId(String sendBillNo,String orderId) {
             using (OrderDataContext context = new OrderDataContext(db)) {
                 var od = from shipping in context.shipping
                           where shipping.sendBillNo == sendBillNo &&
                           shipping.orderId == orderId
                           group shipping by shipping.shippingRequestId into shipping
                           select new ShippingRequest {
                                    requestId = shipping.Key};
                    return od.ToList<ShippingRequest>();                           
             }
         }

         public Object findShippingByBillNumAndSendCompanyG(String sendBillNo, String company) {
             OrderDataContext context = new OrderDataContext(db);             
             var od = from shipping in context.shipping
                      where shipping.sendBillNo == sendBillNo &&
                      shipping.sendCompany == company
                      group shipping by new { shipping.sendBillNo, shipping.receivedDate, shipping.preReceivedDate, shipping.sendCompany, shipping.shippingRequestId }
                          into shipping
                          select new
                          {
                              shippingRequestId = shipping.Key.shippingRequestId,
                              sendCompany = shipping.Key.sendCompany,
                              sendBillNo = shipping.Key.sendBillNo,
                              preReceivedDate = shipping.Key.preReceivedDate,
                              receivedDate = shipping.Key.receivedDate
                          };

             using (context.Connection)
             {
                 return od.ToList();
             }
         }
        
        public List<Shipping> findShippingByBillNumAndSendCompany(String sendBillNo, String company) {
            using (OrderDataContext context = new OrderDataContext(db))
            {
                 var od = from shipping in context.shipping
                      where shipping.sendBillNo == sendBillNo &&
                                    shipping.sendCompany == company                    
                        select shipping;
                 return od.ToList<Shipping>();
            }
         }

         public void persist(Shipping shipping)
         {
             using (OrderDataContext context = new OrderDataContext(db))
             {
                 context.shipping.InsertOnSubmit(shipping);
                 context.SubmitChanges();
             }
         }

         public void merge(Shipping shipping)
         {
             using (OrderDataContext context = new OrderDataContext(db))
             {
                 context.shipping.Attach(shipping);
                 context.Refresh(RefreshMode.KeepCurrentValues, shipping);
                 context.SubmitChanges();
             }
         }
    }
}
